package org.kitten.core.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * @author uniquejava@gmail.com
 * @since 2007-8-28
 */
public class DbUtil {
    private static final Log logger = LogFactory.getLog(DbUtil.class);

    private DbUtil() {
        // hide constructor
    }

    public static void rollback(Connection conn) {
        if (conn != null) {
            try {
                conn.rollback();
                conn.setAutoCommit(true);
            } catch (SQLException e) {
                logger.error(e);
            }
        }
    }

    public static void setAutoCommit(Connection conn, boolean b) {
        if (conn != null) {
            try {
                conn.setAutoCommit(b);
            } catch (SQLException e) {
                logger.error(e);
            }
        }
    }

    public static void close(ResultSet rs, Statement stmt, Connection conn) {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
        if (stmt != null) {
            try {
                stmt.close();
                stmt = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }

    public static void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
                rs = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }

    public static void close(Statement... stmts) {
        for (int i = 0; i < stmts.length; i++) {
            Statement stmt = stmts[i];
            if (stmt != null) {
                try {
                    stmt.close();
                    stmt = null;
                } catch (Exception e) {
                    logger.error(e);
                }
            }
        }
    }

    public static void close(Connection conn) {
        if (conn != null) {
            try {
                conn.close();
                conn = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }

    /**
     * 如果有事务处理，在关闭连接前需要将autoCommit设置为true.
     * 
     * @param rs
     * @param stmt
     * @param conn
     */
    public static void closeWithTx(Connection conn) {
        if (conn != null) {
            try {
                conn.setAutoCommit(true);
            } catch (Exception e) {
                logger.error(e);
            }
            try {
                conn.close();
                conn = null;
            } catch (Exception e) {
                logger.error(e);
            }
        }
    }
}
